Before Facebook Login in an android app, I’m Submit the request and GraphResponce Object return only ........
GraphResponce : { Response: responseCode: 200, graphObject: {"name":"Development Mindstick","id":"219688002317931"}, error: null}
JSONObject: {"name":"Development Mindstick","id":"219688002317931"}
My Code :
loginButton = (LoginButton) findViewById(R.id.fb_login_button);
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
callbackManager = CallbackManager.Factory.create(); // create instance of callback manager class
if (Utils.isNetworkAvailable(getApplicationContext())) {
loginButton.setReadPermissions(Arrays.asList(EMAIL, PUBLIC_PROFILE));
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
accessTokenFacebook = loginResult.getAccessToken();
final GraphRequest request = GraphRequest.newMeRequest(accessTokenFacebook, new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.i("GraphResponse", response.toString());
Log.i("JSONObject", object.toString());
// Get facebook data from login
String email="";
try {
String userName = object.getString("name");
String id = object.getString("id");
email=object.getString("email"); // Error Generate
String firstName = " ", lastName = " ";
if (userName != null) {
int end = userName.lastIndexOf(' ');
if (end >= 0) {
firstName = userName.substring(0, end);
lastName = userName.substring(end + 1, userName.length());
}
}
Toast.makeText(getApplicationContext(),"Facebook Login Successful",Toast.LENGTH_SHORT).show();
if(email.equals("")) {
Toast.makeText(getApplicationContext(),getResources().getString(R.string.email_permission),Toast.LENGTH_SHORT).show();
}
else {
loginApi("facebook", id, firstName, lastName, email);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
request.executeAsync();
}
@Override
public void onCancel() {
Log.d("facebook Login","On cancel");
}
@Override
public void onError(FacebookException error) {
Log.v("Error", error.toString());
}
});
} else {
Toast.makeText(this, getResources().getString(R.string.no_internet), Toast.LENGTH_SHORT).show();
}Gradle
implementation 'com.facebook.android:facebook-login:[4,5)'
But my need is GraphResponse object also return user email which is not easily possible in my code.
Aryan Kumar
02-Jul-2023Yes, the Facebook Login API returns only the user's name and ID by default. To get more information about the user, such as their email address or profile picture, you need to specify the
fieldsparameter in the login request. For example, the following code will get the user's name, ID, email address, and profile picture:public void loginWithFacebook() { LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email", "public_profile"));
Code snippet
}
The
fieldsparameter can be a comma-separated list of any of the following fields:You can also use the
fieldsparameter to get information about the user's friends, groups, and other data. For more information, see the Facebook Graph API documentation: https://developers.facebook.com/docs/graph-api/reference/user.Arti Mishra
17-Sep-2018After long study, we find the solution of my problem.
First of all, I allow the Facebook permission for our Android app and after that, we add the following code.